/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 666
 * Date: 2022-12-12
 * Time: 20:32
 */
public class test {
}

class Solution {
    public int beautySum(String s) {
        int l = s.length();


        int ret = 0;
        for (int i = 0; i < l; i++) {
            int[] arr = new int[26];
            int max = 0;
            for (int j = i; j < l; j++) {
                arr[s.charAt(j) - 'a']++;
                max = Math.max(max, arr[s.charAt(j) - 'a']);

                int min = s.length();
                for (int k = 0; k < 26; k++) {
                    if (arr[k] > 0) {
                        min = Math.min(min, arr[k]);
                    }

                }
                ret += max - min;
            }
        }
        return ret;
    }
}